// pages/Detail/Detail.ts
// 白利帅  商品详情
import { Details } from '../../api/category'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        id: 0,//传过来的商品id
        list: [],//存放商品详情的数据
        price: 0,//价格
        name: '',//名字
        img: '',//图片
        cid: 0,//goods_id
        allnum: 0,//购物车数量,
        fall: false,//收藏的状态
    } as any,

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(e: any) {
        this.data.arr = wx.getStorageSync('cartData') || []
        this.setData({
            id: e.id,
            arr: this.data.arr
        })
        // 调用商品详情接口
        this.Details()
        // 购物车数量
        this.sdd()
        // 收藏
        let arr = wx.getStorageSync('showList') || []
        const index = arr.findIndex((item: any) => item.id == this.data.id)
        if (index > -1) {
            this.setData({
                fall: true
            })
        }
        // 浏览记录

        
    },
    //   封装商品请求
    Details() {
        Details({ goods_id: this.data.id }).then((res: any) => {
            console.log(res);
            let histroyList = wx.getStorageSync("roadList") || []
            histroyList = [res.message,...histroyList]
            this.setData({
                list: res.message,
                price: res.message.goods_price,
                name: res.message.goods_name,
                img: res.message.pics_big_url,
                cid: res.message.goods_id
            })
            wx.setStorageSync("roadList",histroyList)

        })
    },
    //预览图片点击事件  .
    preivw(e: any) { //预览图片点击事件
        let { url } = e.currentTarget.dataset
        let arr: any = []
        this.data.list.pics.forEach((item: any) => {
            arr.push(item.pics_big_url)
        })
        wx.previewImage({
            urls: arr,
            current: url
        })
    },
    // 点击收藏
    collect() {
        //     this.setData({   
        //         fall:!this.data.fall
        //   })

        let arr = wx.getStorageSync('showList') || []
        if (this.data.fall) {
            const index = arr.findIndex((item: any) => item.id == this.data.id)
            arr.splice(index, 1)
            this.setData({ fall: false })
        } else {
            let obj = {
                price: this.data.list.goods_price,
                name: this.data.list.goods_name,
                img: this.data.list.goods_big_logo,
                id: this.data.cid
            }
            arr.push(obj)
            this.setData({ fall: true })
        }
        wx.setStorageSync('showList', arr)
    },
    //   加入购物车
    jump() {
        let arr = wx.getStorageSync('cartData') || []
        // console.log(arr);
        const index = arr.findIndex((item: any) => item.id == this.data.id)
        if (index > -1) {
            arr[index].num += 1
        } else {
            let obj = {
                price: this.data.list.goods_price,
                title: this.data.list.goods_name,
                url: this.data.list.goods_big_logo,
                num: 1,
                check: false,
                id: this.data.cid
            }
            arr.push(obj)
        }
        wx.setStorageSync('cartData', arr)
    },
    // 购物车数量
    sdd() {
        let arr = wx.getStorageSync('cartData') || []
        this.setData({
            allnum: arr.length
        })
    },
    // 跳转购物车
    shopcart() {
        wx.switchTab({
            url: "../../pages/cart/cart"
        })
    }

})